<template>
  <el-dialog
    :visible.sync="visible"
    title="整改建议"
    append-to-body
    width="700px"
    @close="handleClose"
  >
    <div class="container">
      <el-form label-width="50px" size="mini">
        <el-form-item label="标题" required>
          <el-input v-model="title" />
        </el-form-item>

        <el-form-item label="内容" required>
          <template v-if="visible">
            <Editor ref="EditorRef" :height="300" />
          </template>
          <el-link type="primary" @click="handleSelectTemp">
            选择模板
          </el-link>
          <div class="tag-block">
            <template v-for="(tagItem,index) in tagListComputed">
              <span :key="index" class="tag-item" @click="()=> { handleTagSelect(tagItem) }">{{ tagItem }}</span>
            </template>
          </div>
        </el-form-item>
      </el-form>
    </div>

    <div slot="footer" class="footer">
      <el-button size="mini" @click="handleClose">
        取消
      </el-button>
      <el-button size="mini" type="primary" @click="handleOk">
        保存
      </el-button>
    </div>
    <ReformTemplateSelect ref="ReformTemplateSelectRef" />
  </el-dialog>
</template>

<script>
import Editor from '@/components/Tinymce'
import ReformTemplateSelect from './templateSelect'
import { fetchDropTaskTemplate } from '@/api/commonDropDown'
import { isValid } from '../../../../../utils'

const tagList = ['日期', '异常的下级指标']
export default {
  name: 'ReformTemplateInput',
  components: { Editor, ReformTemplateSelect },
  data() {
    return {
      visible: false,
      taskList: [],
      title: '',
      tagList //
    }
  },
  computed: {
    tagListComputed: function() {
      const { businessKpiParams } = this.$store.state.businessFormData.basicForm.data
      return [...tagList, ...businessKpiParams.map(e => e.name)]
    }
  },

  methods: {
    open(val) {
      this.$data.visible = true
      this.postTaskTemplateList()
      this.setEditor(val)
      return new Promise((resolve, reject) => {
        this.monitor = { resolve, reject }
      })
    },
    setEditor(params = {}) {
      this.$nextTick(() => {
        this.$data.title = params.title
        this.$refs.EditorRef.initSuccess().then(() => {
          this.$refs.EditorRef.setContent(params.content)
        })
      })
    },
    getEditor() {
      return this.$refs.EditorRef.getContent()
    },
    postTaskTemplateList() {
      fetchDropTaskTemplate().then(res => {
        this.$data.taskList = res
      })
    },
    handleSelectTemp() {
      this.$refs.ReformTemplateSelectRef.open().then((e) => {
        this.setEditor(e)
      })
    },

    handleTagSelect(e) {
      this.$refs.EditorRef.getEditor().insertContent('<span>@' + e + '@</span>')
    },

    handleClose() {
      this.$data.visible = false
      this.$emit('close')
    },
    handleOk() {
      const content = this.getEditor()
      const { title } = this.$data
      if (!isValid(title)) {
        this.$message.warning('请输入标题')
        return
      }
      if (!isValid(content)) {
        this.$message.warning('请输入内容')
        return
      }
      this.monitor.resolve({ title, content })
      this.handleClose()
    }

  }
}
</script>

<style lang="scss" scoped>
  .select{
    color: red;
  }
  .tag-block{
    margin-top: 20px;
    .tag-item{
      background-color: #f8f8f8;
      border: 1px solid #d9ddee;
      margin-right: 10px;
      margin-bottom: 4px;
      padding: 1px 5px;
      border-radius: 3px;
      font-size: 12px;
      cursor: pointer;
    }
  }
  .temp-block {
    margin-top: 20px;

    .title {
      margin-bottom: 10px;
      color: #666;
    }
    .temp-item {
      background-color: #fafbff;
      border: 1px solid #d9ddee;
      margin-bottom: 4px;
      padding: 5px 5px;
      border-radius: 3px;
      font-size: 12px;
      cursor: pointer;

    }
  }
  .tox-tinymce-aux {
      z-index: 11111;
  }

</style>
